package com.fancy.learncenter.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.MaterialDetailDataBean;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.Utils;
import com.fancy.learncenter.utils.WXShareUtil;
import com.fancy.learncenter.view.PopupWindowShare;
import com.superservice.lya.R;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class DataDetailActivity extends BaseActivity {

    String dataId;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.simpleDraweeView)
    SimpleDraweeView simpleDraweeView;
    @Bind(R.id.mWebView)
    WebView mWebView;
    @Bind(R.id.title_tv)
    TextView titleTv;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.rootView)
    LinearLayout rootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("资料");


        mWebView.getSettings().setJavaScriptEnabled(true);

        dataId = getIntent().getStringExtra("dataId");

        setRightImg(R.mipmap.ic_fenxiang, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindowShare == null) {
                    popupWindowShare = new PopupWindowShare(DataDetailActivity.this, rootView);
                    popupWindowShare.setOnClickCallBack(new PopupWindowShare.OnClickCallBack() {
                        @Override
                        public void wxFriendCallBack() {
                            WXShareUtil.getInstance(DataDetailActivity.this).shareWebPage(shareUrlPic, shareTitle, shareContent, shareLinkUrl, true);
                        }

                        @Override
                        public void wxCallBack() {
                            WXShareUtil.getInstance(DataDetailActivity.this).shareWebPage(shareUrlPic, shareTitle, shareContent, shareLinkUrl, false);
                        }
                    });
                }
                popupWindowShare.showAtLocation();

            }
        });

        getAccount();

    }

    PopupWindowShare popupWindowShare;

    String shareUrlPic = "";
    String shareLinkUrl = "";
    String shareTitle = "";
    String shareContent = "";

    private void getAccount() {
        Map maps = new HashMap();
        maps.put("dataId", dataId);
        HttpMehtod.getInstance().dataDetail(maps, new IdeaObserver<BaseDataBean<MaterialDetailDataBean>>(this, true) {

            @Override
            public void onSuccess(BaseDataBean<MaterialDetailDataBean> listBaseDataBean) {

                simpleDraweeView.setImageURI(listBaseDataBean.getData().getImgsrc());

                name.setText(listBaseDataBean.getData().getNick_name());

                titleTv.setText(listBaseDataBean.getData().getTitle());


                shareUrlPic = listBaseDataBean.getData().getShare_img();
                shareContent = listBaseDataBean.getData().getShare_content();
                shareLinkUrl = listBaseDataBean.getData().getShare_link();
                shareTitle = listBaseDataBean.getData().getShare_title();

                mWebView.loadDataWithBaseURL(null, Utils.getHtmlData(listBaseDataBean.getData().getContent()), "text/html", "utf-8", null);


            }

            @Override
            public void onFail(int errorCode) {

            }

        });
    }
}
